﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuKou.Web.Core;
using System.Web;

namespace LuKou.Web.UserAuth
{
    public class AdminMembership : UserMembership
    {
        protected override string CookieName
        {
            get { return WebConfig.PrefixKey + "AdminCookie"; }
        }

        public override void Write(UserModel user, DateTime expressTime)
        {
            User = user;
            HttpCookie cookie = new HttpCookie(CookieName);
            cookie.Value = SecurityCookie.EncryptToBase64(user, WebConfig.AdminPasswordKey);
            if (expressTime != DateTime.MaxValue && expressTime != DateTime.MinValue)
            {
                cookie.Expires = expressTime;
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public override void Read()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(CookieName);
            if (cookie == null)
            {
                base.User = new UserModel();
            }
            else
            {
                CookieString = cookie.Value;
                base.User = SecurityCookie.DecryptFromBase64(cookie.Value, WebConfig.AdminPasswordKey);
            }
            try
            {
                //Authority.Fill(base.User.Code);
            }
            catch (Exception e)
            {
                throw new Exception("无法将数据库中的权限数据转换为数据实体", e);
            }
        }
    }
}
